package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysProduct;
import com.ruoyi.system.domain.SysProject;
import com.ruoyi.system.service.*;
import com.ruoyi.web.vo.AdminDaliyView;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品Controller
 * 
 * @author ruoyi
 * @date 2020-12-28
 */
@Controller
@RequestMapping("/system/product")
public class SysProductController extends BaseController
{
    private String prefix = "system/product";

    @Autowired
    private ISysProductService sysProductService;
    @Autowired
    private ISysProjectService sysProjectService;
    @Autowired
    private IAdminDaliyService adminDaliyService;
    @Autowired
    private IAdminWeeklyJobService adminWeeklyJobService;
    @Autowired
    private IAdminWeeklyService adminWeeklyService;

    @RequiresPermissions("system:product:view")
    @GetMapping()
    public String product()
    {
        return prefix + "/product";
    }

    /**
     * 查询日报列表
     */
    @GetMapping("/{productType}")
    @ResponseBody
    public AjaxResult getProductData(@PathVariable("productType") Long productType)
    {
        List list = new ArrayList();
        if (0 == productType) {
            list = sysProductService.selectSysProductList(new SysProduct());
        } else {
            for (SysProject project:sysProjectService.selectSysProjectList(new SysProject())) {
                SysProduct product = new SysProduct();
                product.setProductId(project.getProjectId());
                product.setProductName(project.getProjectName());
                list.add(product);
            }
        }
        AjaxResult ajax = AjaxResult.success(JSONArray.toJSON(list));
        return ajax;
    }

    /**
     * 查询产品统计列表
     */
    @RequiresPermissions("system:daliy:list")
    @PostMapping("/statistics")
    @ResponseBody
    public TableDataInfo statistics(AdminDaliyView adminDaliyView)
    {
        startPage();
        List<AdminDaliyView> list = adminDaliyService.selectAdminDaliyListStatistics(adminDaliyView);
        return getDataTable(list);
    }

    /**
     * 查询产品列表
     */
    @RequiresPermissions("system:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysProduct sysProduct)
    {
        startPage();
        List<SysProduct> list = sysProductService.selectSysProductList(sysProduct);
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @RequiresPermissions("system:product:export")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysProduct sysProduct)
    {
        List<SysProduct> list = sysProductService.selectSysProductList(sysProduct);
        ExcelUtil<SysProduct> util = new ExcelUtil<SysProduct>(SysProduct.class);
        return util.exportExcel(list, "product");
    }

    /**
     * 新增产品
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品
     */
    @RequiresPermissions("system:product:add")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysProduct sysProduct)
    {
        return toAjax(sysProductService.insertSysProduct(sysProduct));
    }

    /**
     * 修改产品
     */
    @GetMapping("/edit/{productId}")
    public String edit(@PathVariable("productId") Long productId, ModelMap mmap)
    {
        SysProduct sysProduct = sysProductService.selectSysProductById(productId);
        mmap.put("sysProduct", sysProduct);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品
     */
    @RequiresPermissions("system:product:edit")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysProduct sysProduct)
    {
        return toAjax(sysProductService.updateSysProduct(sysProduct));
    }

    /**
     * 删除产品
     */
    @RequiresPermissions("system:product:remove")
    @Log(title = "产品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysProductService.deleteSysProductByIds(ids));
    }
}
